package files_controller_with_factory;
import java.io.*;
import org.apache.log4j.Logger;

public class DeleteCommandExecutor implements ICommandExecutor {
	private static final Logger cont_log = Logger.getLogger("control");

	public void execute(String string){
		String[] str_mas = string.split(" ");
		String path = str_mas[1];
		File folder = new File(path);
		if(!folder.exists()){
			cont_log.warn("Fellow, it's wrong way");
			return;
		} else Deleting(folder);
			cont_log.info("Success deleting");
	}
	
	private void Deleting(File f){			
		if(f.isDirectory()){
			File[] list = f.listFiles();
			for(File file : list){
				Deleting(file);
			}
			f.delete();
		}else f.delete();
	
				
		/*if(f.isFile()){
			f.delete();
		} else if(f.isDirectory()){
			File[] list = f.listFiles();
			for(File file : list){
				Deleting(file);
				file.delete();
			}
			f.delete();
		}*/
	}
	
	private static DeleteCommandExecutor instance;
	private DeleteCommandExecutor(){			
	}
	public static ICommandExecutor getInstance(){
		if(instance==null){
			instance = new DeleteCommandExecutor(); 
		}
		return instance;
	}
}
